package file

import (
	"bytes"
	"github.com/nfnt/resize"
	"image"
	"image/png"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

// ConvertToPDF
// @Description: 转换文件为pdf
// @param filePath 需要转换的文件
// @param outPath 转换后的PDF文件存放目录
// @return string
func ConvertToPDF(filePath string, newFilePath string) bool {

	var outPath, _ = filepath.Split(newFilePath)
	// 1、拼接执行转换的命令
	commandName := ""
	var params []string
	if runtime.GOOS == "windows" {
		commandName = "cmd"
		params = []string{"/c", "soffice", "--headless", "--invisible", "--convert-to", "pdf", filePath, "--outdir", outPath}
	} else if runtime.GOOS == "linux" {
		commandName = "libreoffice"
		params = []string{"--invisible", "--headless", "--convert-to", "pdf", filePath, "--outdir", outPath}
	}
	// 开始执行转换
	if _, ok := interactiveToexec(commandName, params); ok {
		_, err := os.Stat(newFilePath)
		if err != nil {
			return false
		}
		return true
	} else {
		return false
	}
}

// ConvertToPNG
// @Description: 转换文件为pdf
// @param filePath 需要转换的文件
// @param outPath 转换后的PDF文件存放目录
// @return string
func ConvertToPNG(filePath string, newFilePath string) bool {

	var outPath, _ = filepath.Split(newFilePath)
	// 1、拼接执行转换的命令
	commandName := ""
	var params []string
	if runtime.GOOS == "windows" {
		commandName = "cmd"
		params = []string{"/c", "soffice", "--headless", "--invisible", "--convert-to", "png", filePath, "--outdir", outPath}
	} else if runtime.GOOS == "linux" {
		commandName = "libreoffice"
		params = []string{"--invisible", "--headless", "--convert-to", "pdf", filePath, "--outdir", outPath}
	}
	// 开始执行转换
	if _, ok := interactiveToexec(commandName, params); ok {
		_, err := os.Stat(newFilePath)
		if err != nil {
			return false
		}
		return true
	} else {
		return false
	}
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

// interactiveToexec
// @Description: 执行指定命令
// @param commandName 命令名称
// @param params 命令参数
// @return string 执行结果返回信息
// @return bool 是否执行成功
func interactiveToexec(commandName string, params []string) (string, bool) {
	cmd := exec.Command(commandName, params...)
	buf, err := cmd.Output()
	w := bytes.NewBuffer(nil)
	cmd.Stderr = w
	if err != nil {
		log.Println("Error: <", err, "> when exec command read out buffer")
		return "", false
	} else {
		return string(buf), true
	}
}

// ImageZoom 按宽度缩放图片
// @Description: 执行指定命令
// @param data 图片数据
// @param width 宽度
// @return height 高度
// @return bool 是否执行成功
func ImageZoom(data []byte, width, height uint) ([]byte, error) {
	reader := bytes.NewReader(data)

	var img, _, err = image.Decode(reader)

	if err != nil {
		return nil, err
	}

	thImg := resize.Resize(width, height, img, resize.Lanczos3)
	buf := new(bytes.Buffer)
	err = png.Encode(buf, thImg)
	if err != nil {
		log.Fatal(err)
		return nil, err
	}
	return buf.Bytes(), nil
}

// GetExt 获取文件后缀,不带电
func GetExt(fileName string) string {
	var s = filepath.Ext(fileName)
	s = strings.ToLower(s)
	if strings.HasPrefix(s, ".") {
		s = s[1:]
	}
	return s
}
